package sk.openit.xml.stax.iterator;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.List;

import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartDocument;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

import sk.openit.xml.entity.DbConfig;
import sk.openit.xml.entity.DbConfigTags;

public class DbConfigWriter implements DbConfigTags {
	private String fileName;

	public DbConfigWriter(String fileName) {
		this.fileName = fileName;
	}

	public void serialize(DbConfig dbConfig) {
		try {
			XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
			XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(fileName));

			XMLEventFactory eventFactory = XMLEventFactory.newInstance();
			XMLEvent end = eventFactory.createDTD("\n");

			StartDocument startDocument = eventFactory.createStartDocument();
			eventWriter.add(startDocument);
			eventWriter.add(end);

			Attribute attribute = eventFactory.createAttribute(DB_TYPE, dbConfig.getDbType());
			List attributeList = Arrays.asList(attribute);
			    
			StartElement configStartElement = eventFactory.createStartElement("", "", CONFIG, attributeList.iterator(), null);
			eventWriter.add(configStartElement);
			eventWriter.add(end);

			createNode(eventWriter, HOST, dbConfig.getHost());
			createNode(eventWriter, PORT, dbConfig.getPort());
			createNode(eventWriter, INSTANCE, dbConfig.getInstance());
			createNode(eventWriter, DATABASE, dbConfig.getDatabase());
			createNode(eventWriter, USER, dbConfig.getUser());

			eventWriter.add(eventFactory.createEndElement("", "", CONFIG));
			eventWriter.add(end);
			eventWriter.add(eventFactory.createEndDocument());
			eventWriter.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (FactoryConfigurationError e) {
			e.printStackTrace();
		} catch (XMLStreamException e) {
			e.printStackTrace();
		}	
	}
	
	private void createNode(XMLEventWriter eventWriter, String name, String value) throws XMLStreamException {

		XMLEventFactory eventFactory = XMLEventFactory.newInstance();
		XMLEvent end = eventFactory.createDTD("\n");
		XMLEvent tab = eventFactory.createDTD("\t");
		// Create Start node
		StartElement sElement = eventFactory.createStartElement("", "", name);
		eventWriter.add(tab);
		eventWriter.add(sElement);
		// Create Content
		Characters characters = eventFactory.createCharacters(value);
		eventWriter.add(characters);
		// Create End node
		EndElement eElement = eventFactory.createEndElement("", "", name);
		eventWriter.add(eElement);
		eventWriter.add(end);

	}
}
